package com.power.dao.impl;

import com.power.dao.UserDao;
import com.power.ov.ResultDataTable;
import com.power.pojo.Roles;
import com.power.pojo.Users;
import com.power.utils.JdbcBuild;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/04 23:00
 * Description:
 * Version: V1.0
 */
public class UserDaoImpl extends JdbcBuild implements UserDao {
    @Override
    public List<Map<String, Object>> findByAccount(String user_account, String old_pwd) throws SQLException {
        String sql="select * from users where user_account=? and user_pwd=?";
        Object[] params={user_account,old_pwd};
        List<Map<String,Object>> list=super.findAllForList(sql,params);
        return list;
    }

    @Override
    public int userUpdate(Users users) throws SQLException {

        String sql="update users set user_pwd=? , sex=? , age=? , user_phone=? ,user_email=? where user_account=?";
        Object[] params={users.getUser_pwd(),users.getSex(),users.getUser_age(),users.getUser_tel(),users.getUser_email(),users.getUser_account()};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public List<Map<String, Object>> queryRole() throws SQLException {
        String sql="select * from role,states where role.role_state=states.state_id";
        List<Map<String, Object>> list=super.findAllForList(sql,null);
        return list;
    }

    @Override
    public int roleUpdate(Roles roles) throws SQLException {
        String sql="update role set role_name=?,role_state=?,update_time=? where role_id=?";
        Object[] params={roles.getRole_name(),roles.getRole_state(),roles.getUpdate_time(),roles.getRole_id()};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public int findByRoleName(String roleName) throws SQLException {
        String sql="select count(*) from role where role_name=?";
        Object[] params={roleName};
        int count=super.findAllForInt(sql,params);
        System.out.println("count:"+count);
        return count;
    }

    @Override
    public int addRole(String roleName, Date date) throws SQLException {
        String sql="insert into role(role_name,update_time) values(?,?)";
        Object[] params={roleName,date};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public List<Map<String, Object>> queryByNameAndState(String roleName, String roleStatee) throws SQLException {
        String sql="select * from role,states where role.role_state=states.state_id ";
        List list=new ArrayList();
        if(roleName !=null && !"".equals(roleName)){
            sql+=" and role.role_name=? ";
            list.add(roleName);
        }
        if(roleStatee !=null && !"".equals(roleStatee)){
            sql+=" and role.role_state=? ";
            list.add(roleStatee);
        }
        Object[] params=list.toArray();
        List<Map<String,Object>> list1=super.findAllForList(sql,params);
        return list1;
    }

    @Override
    public List<Map<String, Object>> userQuery(String page, String limit) throws SQLException {
        int newPage=Integer.parseInt(page);
        int newLimit=Integer.parseInt(limit);
        String sql="select * from users ,states,role  where users.user_state=states.state_id and is_delete=1 AND users.role_id=role.role_id limit ?,?";
        Object[] params={(newPage-1)*newLimit,newLimit};
        List<Map<String,Object>> list=super.findAllForList(sql,params);
        return list;
    }

    @Override
    public int userCount() throws SQLException {
        String sql="select count(*) from users where user_state=1 and is_delete=1";
        int count=super.findAllForInt(sql,null);
        return count;
    }

    @Override
    public int updateUser(Users users) throws SQLException {
        String sql="update users set sex=?,user_name=?,user_state=?,in_time=?,out_time=?,user_email=?,role_id=? where user_account=?";
        Object[] params={users.getSex(),users.getUser_name(),users.getUser_state(),
                users.getEntry_time(),users.getQuit_time(),users.getUser_email(),
                users.getRole_id(),users.getUser_account()};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public int addUser(Users users) throws SQLException {
        String sql="insert into users(user_account,user_name,user_pwd," +
                "user_phone,user_email,sex,role_id,in_time,out_time)" +
                "values(?,?,?,?,?,?,?,?,?)";
        Object[] params={users.getUser_account(),users.getUser_name(),users.getUser_pwd(),
                users.getUser_tel(),users.getUser_email(),users.getSex(),users.getRole_id(),users.getEntry_time(),users.getQuit_time()};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public List<Map<String, Object>> queryByUser(String user_name, String user_state, String page, String limit) throws SQLException {
        int newPage=Integer.parseInt(page);
        int newLimit=Integer.parseInt(limit);
        String sql="select * from users,states,role where users.is_delete=1 and users.user_state=states.state_id and users.role_id=role.role_id";
        List list=new ArrayList();
        if(user_name !=null && !"".equals(user_name)){
            sql+=" and user_name=? ";
            list.add(user_name);
        }
        if(user_state !=null && !"".equals(user_state)){
            sql+=" and user_state=? ";
            list.add(user_state);
        }
        sql+=" limit ?,?";
        list.add((newPage-1)*newLimit);
        list.add(newLimit);
        Object[] params=list.toArray();
        List<Map<String, Object>> list1=super.findAllForList(sql,params);
        return list1;
    }

    @Override
    public int queryByUserCount(String user_name, String user_state) throws SQLException {
        String sql="select count(*) from users where is_delete=1";
        List list=new ArrayList();
        if(user_name !=null && !"".equals(user_name)){
            sql+=" and user_name=? ";
            list.add(user_name);
        }
        if(user_state !=null && !"".equals(user_state)){
            sql+=" and user_state=? ";
            list.add(user_state);
        }
        Object[] params=list.toArray();
        int count=super.findAllForInt(sql,params);
        return count;
    }



    @Override
    public int deleteUser(String userAccount) throws SQLException {
        String sql="update users set is_delete=0 where user_account=?";
        Object[] params={userAccount};
        int res=super.executeUpdate(sql,params);
        return res;
    }

    @Override
    public List<Map<String, Object>> judgeUserAcc(String userAccount) throws SQLException {
        String sql="select * from users where user_account=? and is_delete=1";
        Object[] params={userAccount};
        List<Map<String,Object>> list=super.findAllForList(sql,params);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryAuthorityAll(String roleId) throws SQLException {
        String sql="select  permission.permission_id AS value , permission.permission_des as title FROM permission,role_permission WHERE role_permission.permission=permission.permission_id and role_permission.role_id=?";
        Object[] params={roleId};
        List<Map<String, Object>> list=super.findAllForList(sql,params);
        return list;
    }


    @Override
    public List<Map<String, Object>> queryAuthorityAll2() throws SQLException {
        String sql="select  permission.permission_id AS value ,  permission.permission_des as title FROM permission";
        List<Map<String, Object>> list=super.findAllForList(sql,null);
        return list;
    }

    @Override
    public int updateAuthority(String roleId, List<Map> list) throws SQLException {
        String sql="delete from role_permission where role_id=?";
        String sqlin="insert into role_permission(role_id,permission) values(?,?)";
        Object[] params={roleId};
        int delete=super.executeUpdate(sql,params);
        int res=0;
            for (Map map:list) {
                List list1=new ArrayList();
                String per= map.get("value").toString();
                list1.add(roleId);
                list1.add(per);
                Object[] params2=list1.toArray();
                res=super.executeUpdate(sqlin,params2);
            }
        return res;
    }



}
